package com.haiermzj.client;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.haiermzj.client.utils.Contance;


/**
 * @ClassName: SocketTestActivity
 * @Description: 测试封装的SocketConnect和接收回调接口
 */
public class SocketConnectTestActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "SocketConnectTestActivity";

    //android模拟器把127.0.0.1当做自己的IP， 把10.0.2.2就能成功访问你的电脑本机
    public static final String HOST = "192.168.199.105";

    // 远程tcp socket服务端口
    public static final int PORT = 8888;


    private static ImageView image;
    private static TextView text;
    private static EditText mEditText;
    private static Button connectBtn;
    private static Button sendBtn;
    private static Button disconnectBtn;

    // 封装的socket byte[]收发
    private TestSocketCallback callback;
    private SocketConnect connect;
    private Button openCamera;


    /**
     * 接收线程发送过来的消息，并用TextView显示
     */
    private static Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage----");

//            image.setImageBitmap(((Bitmap) msg.obj));
            String str = (String) msg.obj;
            text.setText(str);
        }
    };
    private static Button takePic;
    private static Button callBackPic;
    private Button riqiBt;
    private TextView riqiTv;
    private Button shijianBt;
    private TextView shijianTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket_test);
        openCamera = (Button) findViewById(R.id.openCamera);
        openCamera.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                socketSendStr(Contance.OPEN_CAMERA);

            }
        });
        mEditText = (EditText) findViewById(R.id.socketEditTxt);
        image = (ImageView) findViewById(R.id.socketSeverReturnImage);
        text = (TextView) findViewById(R.id.socketSeverReturnTxtV);
        sendBtn = (Button) findViewById(R.id.socketSendBtn);
        callBackPic = (Button) findViewById(R.id.callBackPic);
        takePic = (Button) findViewById(R.id.takePic);
        connectBtn = (Button) findViewById(R.id.socketConnectBtn);
        disconnectBtn = (Button) findViewById(R.id.socketDisconnectBtn);
        riqiBt = (Button) findViewById(R.id.riqi_bt);
        riqiTv = (TextView) findViewById(R.id.riqi_tv);
        shijianBt = (Button) findViewById(R.id.shijian_bt);
        shijianTv = (TextView) findViewById(R.id.shijian_tv);
        riqiBt.setOnClickListener(this);
        shijianBt.setOnClickListener(this);
        takePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                socketSendStr(Contance.TAKE_PIC);
            }
        });
        connectBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                SocketConnect();
            }
        });

        sendBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                socketSend();
            }
        });


        disconnectBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                socketClose();
            }
        });
        callBackPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                socketSendStr(Contance.CALLBACKPIC);
            }
        });

    }


    private void SocketConnect() {
//        Log.i(TAG, "SocketConnect----");

        if (connect == null) {
            callback = new TestSocketCallback(mhandler);
            connect = new SocketConnect(callback,this);
            connect.setRemoteAddress(HOST, PORT);
            new Thread(connect).start();

        } else {
            ShowDialog("socket is not null, no need connect again!");
        }
    }


    private void socketSend() {
//        Log.i(TAG, "socketSend----");

        String msg = mEditText.getText().toString();
        if (connect != null) {
            connect.send(msg.getBytes());
        } else {
            ShowDialog("socketConnect is null, need init！");
        }
    }

    private void socketSendStr(String msg) {
//        Log.i(TAG, "socketSendStr----");
        if (connect != null) {
            connect.send(msg.getBytes());
        } else {
            ShowDialog("socketConnect is null, need init！");
        }
    }

    private void socketClose() {
//        Log.i(TAG, "socketClose----");

        if (connect != null) {
            connect.disconnect();
            connect = null;
        }/*else{
            ShowDialog("socketConnect is null, not need close！");
		}*/
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        socketClose();
    }

    /**
     * @param msg
     * @return void
     * @Description: 弹出AlertDialog
     */
    public void ShowDialog(String msg) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(msg)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
    }

    @Override
    public void onClick(View view) {
    }

}


